#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "getch.h"
#include "login.h"
#include "game_function.h"

static void show(int);

/*
 *@brief        主菜单界面
 *@return       登陆成功返回用户名地址，退出返回NULL
 */
char *menu_show(struct data *p_data)
{
    int list = 0;
    char *login_name;
    char ch;

    HIDE_CURSOR();

    system("clear");
  
    while (1) {
        show(list);
        while ((ch = getch()) != ENTER) {
    
            switch (ch) {
                case UP:
                list--;
                break;

                case DOWN:
                list++;
                break;

                default:
                break;
            }

            if (list < 0)
                list = 3;
            if (list > 3)
                list = 0;

            show(list);
        }

        switch (list) {
            case LOGIN:
                system("clear");
                login_name = login(p_data);

                if (login_name != NULL) {
                   return login_name;
                }
                break;

            case RANK:
                system("clear");
                rank(p_data);
                getch();
                break;

            case SIGN_IN:
                system("clear");
                sign_in(p_data);
                getch();
                break;

            case EXIT:
                system("clear");
                SHOW_CURSOR();
                return NULL;

            default:
                break;
        }
    }
}

void show(int list) 
{
    HIDE_CURSOR();

    switch (list) {
        case LOGIN:
        system("clear");
        printf("\n\n\n");
        printf("\t\t\t\t\b\b★---主菜单---★\n");
	    printf("================================================================================\n");
        printf("\n\n");
		printf("            -------- ☆        ▶ 登陆游戏            ☆-------- \n");
        printf("\n");
		printf("            -------- ☆          排 行 榜            ☆-------- \n");
        printf("\n");
	    printf("            -------- ☆          注    册            ☆-------- \n");
        printf("\n");
		printf("            -------- ☆          退    出            ☆-------- \n");
        printf("\n\n");
		printf("================================================================================\n");
        printf("按↑↓键选择，Enter键进入...\n");
        break;

    case RANK:
        system("clear");
        printf("\n\n\n");
        printf("\t\t\t\t\b\b★---主菜单---★\n");
	    printf("================================================================================\n");
        printf("\n\n");
		printf("            -------- ☆          登陆游戏            ☆-------- \n");
        printf("\n");
		printf("            -------- ☆        ▶ 排 行 榜            ☆-------- \n");
        printf("\n");
	    printf("            -------- ☆          注    册            ☆-------- \n");
        printf("\n");
		printf("            -------- ☆          退    出            ☆-------- \n");
        printf("\n\n");
		printf("================================================================================\n");
        printf("按↑↓键选择，Enter键进入...\n");
        break;

    case SIGN_IN:
        system("clear");
        printf("\n\n\n");
        printf("\t\t\t\t\b\b★---主菜单---★\n");
	    printf("================================================================================\n");
        printf("\n\n");
		printf("            -------- ☆          登陆游戏            ☆-------- \n");
        printf("\n");
		printf("            -------- ☆          排 行 榜            ☆-------- \n");
        printf("\n");
		printf("            -------- ☆        ▶ 注    册            ☆-------- \n");
        printf("\n");
        printf("            -------- ☆          退    出            ☆-------- \n");
        printf("\n\n");
		printf("================================================================================\n");
        printf("按↑↓键选择，Enter键进入...\n"); 
        break;

    case EXIT:
        system("clear");
        printf("\n\n\n");
        printf("\t\t\t\t\b\b★---主菜单---★\n");
	    printf("================================================================================\n");
        printf("\n\n");
		printf("            -------- ☆          登陆游戏            ☆-------- \n");
        printf("\n");
		printf("            -------- ☆          排 行 榜            ☆-------- \n");
        printf("\n");
	    printf("            -------- ☆          注    册            ☆-------- \n");
        printf("\n");
		printf("            -------- ☆        ▶ 退    出            ☆-------- \n");
        printf("\n\n");
		printf("================================================================================\n");
        printf("按↑↓键选择，Enter键进入...\n"); 
        break;
    
    default:
        break;
    }
}

